﻿<!DOCTYPE HTML>
<html lang="zh">
<head>
<title>MenuSelect - 语法 &amp; 使用 | AutoHotkey v2</title>
<meta name="description" content="The MenuSelect function invokes a menu item from the menu bar of the specified window." />
<meta name="ahk:equiv-v1" content="commands/WinMenuSelectItem.htm" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<link href="../static/theme.css" rel="stylesheet" type="text/css" />
<script src="../static/content.js" type="text/javascript"></script>
<script type="text/javascript">$(function(){0<=window.navigator.userAgent.toLowerCase().indexOf("ucbrowser")&&CaoNiMaDeUc()})</script>
</head>
<body>

<h1>MenuSelect</h1>

<p>Invokes a menu item from the menu bar of the specified window.</p>

<pre class="Syntax"><span class="func">MenuSelect</span> WinTitle, WinText, Menu <span class="optional">, SubMenu1, SubMenu2, SubMenu3, SubMenu4, SubMenu5, SubMenu6, ExcludeTitle, ExcludeText</span></pre>
<h2 id="Parameters">参数</h2>
<dl>

  <dt>WinTitle</dt>
  <dd>
    <p>类型: <a href="../Concepts.htm#strings">字符串</a>, <a href="../Concepts.htm#numbers">整数</a>或<a href="../Objects.htm">对象</a></p>
    <p>识别目标窗口的窗口标题或其他条件. 请参阅 <a href="../misc/WinTitle.htm">WinTitle</a>.</p>
  </dd>

  <dt>WinText</dt>
  <dd>
    <p>类型: <a href="../Concepts.htm#strings">字符串</a></p>
    <p>如果存在, 此参数必须是目标窗口的单个文本元素的子字符串(和内置的 Window Spy 工具显示的一样). 如果 <a href="DetectHiddenText.htm">DetectHiddenText</a> 为 ON, 那么会检测隐藏文本元素.</p>
  </dd>

  <dt>Menu</dt>
  <dd>
    <p>类型: <a href="../Concepts.htm#strings">字符串</a></p>
    <p>The name (or a prefix of the name) of the top-level menu item, e.g. File, Edit, View. It can also be the position of the desired menu item by using <code>1&amp;</code> to represent the first menu, <code>2&amp;</code> the second, and so on.</p>
    <p>The search is case-insensitive according to the rules of the current user's locale, and stops at the first matching item. The use of ampersand (&amp;) to indicate the underlined letter in a menu item is <em>usually</em> not necessary (i.e. &amp;File is the same as File).</p>
    <p class="warning"><strong>Known limitation:</strong> If the parameter contains an ampersand, it must match the item name exactly, including all non-literal ampersands (which are hidden or displayed as an underline). If the parameter does not contain an ampersand, all ampersands are ignored, including literal ones. For example, an item displayed as "a &amp;&nbsp;b" may match a parameter value of <code>a &amp;&amp;&nbsp;b</code> or <code>a &nbsp;b</code>.</p>
    <p>Specify <code>0&amp;</code> to use the window's <a href="#sys">system menu</a>.</p>
  </dd>

  <dt>SubMenu1</dt>
  <dd>
    <p>类型: <a href="../Concepts.htm#strings">字符串</a></p>
    <p>The name of the menu item to select or its position (see above).</p>
    <p>This can be omitted if the top-level item does not contain a menu (rare).</p>
  </dd>

  <dt>SubMenu2</dt>
  <dd>
    <p>类型: <a href="../Concepts.htm#strings">字符串</a></p>
    <p>If <em>SubMenu1</em> itself contains a menu, this is the name of the menu item inside, or its position.</p>
  </dd>

  <dt>SubMenu3</dt>
  <dt>SubMenu4</dt>
  <dt>SubMenu5</dt>
  <dt>SubMenu6</dt>
  <dd>
    <p>类型: <a href="../Concepts.htm#strings">字符串</a></p>
    <p>Same as above.</p>
  </dd>

  <dt>ExcludeTitle</dt>
  <dd>
    <p>类型: <a href="../Concepts.htm#strings">字符串</a></p>
    <p>标题中含有此参数值的窗口将被排除.</p>
  </dd>

  <dt>ExcludeText</dt>
  <dd>
    <p>类型: <a href="../Concepts.htm#strings">字符串</a></p>
    <p>文本中含有此参数值的窗口将被排除.</p>
  </dd>

</dl>

<h2 id="Error_Handling">错误处理</h2>
<p>如果遇到问题, <a href="../misc/ErrorLevel.htm">ErrorLevel</a> 设置为 1, 否则设置为 0.</p>

<h2 id="Remarks">备注</h2>
<p>For this function to work, the target window need not be active. However, some windows might need to be in a <a href="WinRestore.htm">non-minimized</a> state.</p>
<p>This function <strong>will not work</strong> with applications that use non-standard menu bars. Examples include Microsoft Outlook and Outlook Express, which use disguised toolbars for their menu bars. In these cases, consider using <a href="ControlSend.htm">ControlSend</a> or <a href="PostMessage.htm">PostMessage</a>, which should be able to interact with some of these non-standard menu bars.</p>
<p>The menu name parameters can also specify positions. This method exists to support menus that don't contain text (perhaps because they contain pictures of text rather than actual text). Position 1&amp; is the first menu item (e.g. the File menu), position 2&amp; is the second menu item (e.g. the Edit menu), and so on. Menu separator lines count as menu items for the purpose of determining the position of a menu item.</p>
<p>窗口标题和文本是区分大小写的. 除非 <a href="DetectHiddenWindows.htm">DetectHiddenWindows</a> 被打开, 否则不会检测隐藏窗口.</p>

<h2 id="sys">System Menu</h2>
<p><em>Menu</em> can be <code>0&amp;</code> to select an item within the window's system menu, which typically appears when the user presses <kbd>Alt</kbd>+<kbd>Space</kbd> or clicks on the icon in the window's title bar. 例如:</p>
<pre><em>; Paste a command into cmd.exe without activating the window.</em>
Clipboard := "echo Hello, world!`r"
MenuSelect "ahk_exe cmd.exe",, "0&amp;", "Edit", "Paste"</pre>
<p class="warning"><strong>Caution:</strong> Use this only on windows which have custom items in their system menu.</p>
<p>If the window does not already have a custom system menu, a copy of the standard system menu will be created and assigned to the target window as a side effect. This copy is destroyed by the system when the script exits, leaving other scripts unable to access it. Therefore, avoid using 0&amp; for the standard items which appear on all windows. Instead, post the <a href="https://msdn.microsoft.com/library/ms646360">WM_SYSCOMMAND</a> message directly. 例如:</p>
<pre><em>; Like "<a href="WinMinimize.htm">WinMinimize</a> A", but also play the system sound for minimizing.</em>
WM_SYSCOMMAND := 0x112
SC_MINIMIZE := 0xF020
PostMessage WM_SYSCOMMAND, SC_MINIMIZE, 0,, A</pre>

<h2 id="Related">相关</h2>
<p><a href="ControlSend.htm">ControlSend</a>, <a href="PostMessage.htm">PostMessage</a></p>
<h2 id="Examples">示例</h2>
<div class="ex" id="ExBasic">
<p><a href="#ExBasic">#1</a>: This will select File-&gt;Open in Notepad:</p>
<pre>MenuSelect "Untitled - Notepad", , "File", "Open"

<em>; Same as above except it's done by position vs. name:</em>
MenuSelect "Untitled - Notepad", , "1&amp;", "2&amp;"</pre>
</div>

</body>
</html>